<div style="float:left"><img src="<?php echo base_url("../assets/images/ptpp.jpg") ?>" style="width:200px;"></div>
<div style="width:100%"><h3 style="text-align:center;">Mapping Mandor Selesai
<br /><i>PT. PP (Persero) DVO III</i>
<!-- <br /><i>November 2014</i> -->
</h3>
</div>
    <br clear=both>
<table class="print table table-bordered  ">
    <thead>
        <?php 
    
    
    
        $to_proj = array();
        $header_proyek = "";
        $mintime = time();
        $maxtime = 0;
        foreach($data as $row){
            if (! in_array($row->to_proyek_nama,$to_proj)){
                $header_proyek .= "<th>".$row->to_proyek_nama."</th>";
                $to_proj[] =  $row->to_proyek_nama;
            }
            $timestart = strtotime($row->mapping_tglmulai);
            $timeend = strtotime($row->mapping_tglselesai);
            if ($mintime > $timestart)
                $mintime = $timestart;
            if ($maxtime < $timeend)
                $maxtime = $timeend;
        }

        $currenttime = $mintime;
        $month_arr = array();
        $header_timeline = "";
        while( $currenttime <= $maxtime){
            $currentdate = date("Y-m-d",$currenttime);
            $currentmonth = date("m",$currenttime);
            $currentMonthText = date("M",$currenttime);
            $currentyear = date("Y",$currenttime);
            $month_arr[] = $currentmonth."-".$currentyear;
            
            $currenttime = strtotime($currentdate." +1 month");
            $header_timeline .= "<th>".$currentMonthText."</th>";
        }

        ?>
        
        <tr >
            <th rowspan=2  style="min-width:100px">Proyek</th>
            <th rowspan=2  style="min-width:100px">Mandor</th>
            <th rowspan=2  style="min-width:100px">Renc Selesai</th>
            <th rowspan=2 style="min-width:100px">Pekerjaan</th>
            <th colspan=<?php echo count($to_proj); ?>  style="min-width:200px">Ke Proyek</th>
            <th colspan=<?php echo count($month_arr); ?> style="width:600px">Timeline</th>
        </tr>
        <?php 
            echo $header_proyek;
            echo $header_timeline;
        ?>
    </thead>
    <tbody>
        
<?php
    $i = 0;
    foreach($data as $row){
        $i++;
?>
        <tr>
        <td><?php echo $row->proyek_nama ;?></td>
        <td><?php echo $row->mandor_nama ;?></td>
        <td><?php echo date("d M Y",strtotime($row->monitor_selesai)) ;?></td>
        <td><?php echo $row->pekerjaan_ket ;?></td>
        <?php 
            foreach($to_proj as $proj){
                if ($proj == $row->to_proyek_nama)
                    echo "<td><center>√<center></td>";
                else
                    echo "<td>&nbsp;</td>";
                    
            }
        ?>
        <?php
            $timestart = strtotime($row->mapping_tglmulai);
            $timeend = strtotime($row->mapping_tglselesai);
            
            $monthstart = date("m",$timestart);
            $monthend = date("m",$timeend);
            $monthstarttext = date("M",$timestart);
            $monthendtext = date("M",$timeend);
            $yearstart = date("Y",$timestart);
            $yearend = date("Y",$timeend);
        
            $indexstart  = array_search($monthstart."-".$yearstart , $month_arr);
            $indexend  = array_search($monthend."-".$yearend , $month_arr);
        
            $colsBefore = $indexstart;
            $colsCurrent = $indexend -  $indexstart;
            $colsAfter = count($month_arr) -  $indexstart - 1;
            
            $textstart = $monthstarttext." ".$yearstart;
            $textend = $monthendtext." ".$yearend;
            if ($textstart == $textend)
                $text = $textstart;
            else
                $text = $textstart . " - " . $textend;
        
            if ($colsBefore != 0)
                echo "<td colspan=$colsBefore></td>";
        
            echo "<td colspan=$colsCurrent style='background:#0d0'><center>".$text."<center></td>";
        
            if ($colsAfter != 0)
                echo "<td colspan=$colsAfter></td>";
        ?>
        </tr>
<?php
        
    }
?>
    </tbody>
</table>
